package com.foreknow.controller;

import com.foreknow.po.Cart;
import com.foreknow.service.CartService;
import com.foreknow.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class CartController {
    public Object listCart(HttpServletRequest request){
        //获取前台请求参数  /CartController/listCart?userId=12345671111&businessId=10001
        Cart cart = new Cart();
        String userId = request.getParameter("userId");
        if (request.getParameter("businessId") != null){
            Integer businessId = Integer.parseInt(request.getParameter("businessId"));
            cart.setBusinessId(businessId);
        }
        cart.setUserId(userId);
        List<Cart> list = new ArrayList<>();
        CartService cartService = new CartServiceImpl();
        list = cartService.listCart(cart);
        return list;
    }
    public Object saveCart(HttpServletRequest request){
        //获取前台请求参数  /CartController/saveCart?userId=1234563212&businessId=10002&foodId=7&quantity=3
        String userId = request.getParameter("userId");
        Integer businessId = Integer.parseInt(request.getParameter("businessId"));
        Integer foodId = Integer.parseInt(request.getParameter("foodId"));
        CartService cartService = new CartServiceImpl();
        int result = cartService.saveCart(userId,businessId,foodId);
        return result;
    }
    public Object updateCart(HttpServletRequest request) {
        //获取前台请求参数  /CartController/updateCart?userId=1234563212&businessId=10002&foodId=10&quantity=14
        String userId = request.getParameter("userId");
        Integer businessId = Integer.parseInt(request.getParameter("businessId"));
        Integer foodId = Integer.parseInt(request.getParameter("foodId"));
        Integer quantity = Integer.parseInt(request.getParameter("quantity"));
        CartService cartService = new CartServiceImpl();
        int result = cartService.updateCart(userId, businessId, foodId, quantity);
        return result;
    }
    public Object removeCart(HttpServletRequest request) {
        //获取前台请求参数  /CartController/removeCart?userId=1234563212&businessId=10002&foodId=10
        Cart cart = new Cart();
        String userId = request.getParameter("userId");
        Integer businessId = Integer.parseInt(request.getParameter("businessId"));
        cart.setUserId(userId);
        cart.setBusinessId(businessId);
        if (request.getParameter("foodId") != null){
            Integer foodId = Integer.parseInt(request.getParameter("foodId"));
            cart.setFoodId(foodId);
        }
        CartService cartService = new CartServiceImpl();
        int result = cartService.removeCart(cart);
        return result;
    }
}
